package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.CorAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.JzdxCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgInvestigationResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface JzdxConvert {
    JzdxConvert INSTANCE = Mappers.getMapper(JzdxConvert.class);

    default JzdxCreateDTO create(DcpgInvestigationResultDTO s) {
        JzdxCreateDTO corrector = new JzdxCreateDTO();
        corrector.setXm(s.getBgrxm());
        corrector.setXb(s.getBgrxb());
        corrector.setCsrq(s.getBgrcsrq());
        corrector.setJzlb(s.getNsyjzlb());
        corrector.setZjlx(s.getBgrzjlx());
        corrector.setZjh(s.getBgrzjh());

        Actions.accept(s.getCrimeNameList(),
                v -> corrector.setCrimeInfoList(s.getCrimeNameList().stream().map(JzdxCrimeInfoConvert.INSTANCE::convert).toList()));
        return corrector;
    }

    JzdxEntity createTo(JzdxCreateDTO s);

    JzdxEntity updateTo(JzdxUpdateDTO s);

    JzdxResultDTO convert(JzdxEntity s);

    default JzdxEntity create(JzdxCreateDTO s, Long tenantId, Long deptId, CorAuditActionStatusEnum action) {
        JzdxEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setDeptId(deptId);
        entity.setPreAction(action);
        additionalInformation(entity, s.getNativePlaceList(), s.getResidenceAreaList(), s.getRegisterAreaList());
        return entity;
    }


    default JzdxEntity update(JzdxUpdateDTO s) {
        JzdxEntity entity = updateTo(s);
        additionalInformation(entity, s.getNativePlaceList(), s.getResidenceAreaList(), s.getRegisterAreaList());
        return entity;
    }


    default void additionalInformation(JzdxEntity t, List<String> nativePlaceList, List<String> residenceAreaList, List<String> registerAreaList) {
        if (Objects.isNull(t) || CollectionUtils.isEmpty(nativePlaceList) || CollectionUtils.isEmpty(residenceAreaList) || CollectionUtils.isEmpty(registerAreaList)) {
            return;
        }

        //设置户籍
        t.setGj(nativePlaceList.get(nativePlaceList.size() - 1));

        //设置固定居住地
        t.setGdjzdszs(residenceAreaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        t.setGdjzdszds(residenceAreaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));
        Actions.apply(residenceAreaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH, () -> t.setGdjzdszxq(residenceAreaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX)));
        Actions.apply(residenceAreaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH, () -> t.setGdjzd(residenceAreaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX)));

        //设置户籍所在地
        t.setHjszs(registerAreaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        t.setHjszds(registerAreaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));
        Actions.apply(registerAreaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH, () -> t.setHjszxq(registerAreaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX)));
        Actions.apply(registerAreaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH, () -> t.setHjszd(registerAreaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX)));
    }

    default void convertList(JzdxResultDTO s) {
        List<String> residenceAreaList = new ArrayList<>();
        List<String> registerAreaList = new ArrayList<>();

        //固定居住地，需要保证顺序
        residenceAreaList.add(s.getGdjzdszs());
        residenceAreaList.add(s.getGdjzdszds());
        Actions.apply(StringUtils.isNotEmpty(s.getGdjzdszxq()), () -> residenceAreaList.add(s.getGdjzdszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getGdjzd()), () -> residenceAreaList.add(s.getGdjzd()));

        //户籍地址，需要保证顺序
        registerAreaList.add(s.getHjszs());
        registerAreaList.add(s.getHjszds());
        Actions.apply(StringUtils.isNotEmpty(s.getGdjzdszxq()), () -> registerAreaList.add(s.getHjszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getGdjzd()), () -> registerAreaList.add(s.getHjszd()));

        s.setResidenceAreaList(residenceAreaList);
        s.setRegisterAreaList(registerAreaList);
    }

}
